using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Batman.Cms.Core.Models.Mvc;
using Batman.Core;
using Batman.Jianbei.Forum.Areas.Forum.Responses;
using Batman.Jianbei.Forum.Core.Domains.Socials;
using Batman.Jianbei.Forum.Core.Domains.Users;
using System.Linq;
using Batman.Jianbei.Forum.Areas.Forum.Filters;

namespace Batman.Jianbei.Forum.Services.Socials
{
    public class FollowService : ApplicationService
    {
        public IRepository<Follow, string> FollowRepo { get; set; }

        public IRepository<WeixinUser, string> WxUserRepo { get; set; }

        /// <summary>
        /// 关注某个人
        /// </summary>
        /// <param name="wxUId"></param>
        /// <param name="currentWxUserId"></param>
        /// <returns></returns>
        public ResponseBase Follow(string wxUId, string currentWxUserId)
        {
            bool isFollowing = FollowRepo.GetQuery().Any(t => t.WxUserId == wxUId && t.FollowBy == currentWxUserId);
            if (isFollowing) return new ResponseBase();

            Follow newFollow = new Follow(wxUId, currentWxUserId);

            FollowRepo.Insert(newFollow);

            return new ResponseBase();
        }

        /// <summary>
        /// 取关
        /// </summary>
        /// <param name="wxUId">取关对象</param>
        /// <param name="currentWxUserId">当前登录用户</param>
        /// <returns></returns>
        public ResponseBase Unfollow(string wxUId, string currentWxUserId)
        {
            Follow follow = FollowRepo.GetQuery().FirstOrDefault(t => t.WxUserId == wxUId && t.FollowBy == currentWxUserId);
            if (follow == null) return new ResponseBase();

            FollowRepo.Delete(follow);

            return new ResponseBase();
        }

        /// <summary>
        /// 我的关注列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public PagedResultDto<FollowingItem> GetFollows(PagedResultRequestDto page, FollowFilter filter)
        {
            var query = FollowRepo.GetQuery();
            
            if (!string.IsNullOrEmpty(filter.FollowBy))
            {
                query = query.Where(t => t.FollowBy == filter.FollowBy);
            }

            if (!string.IsNullOrEmpty((filter.WxUserId)))
            {
                query = query.Where(t => t.WxUserId == filter.WxUserId);
            }

            int total = query.Count();

            query = query.OrderByDescending(t => t.CreateAt).Skip(page.SkipCount).Take(page.MaxResultCount);

            var wxUserQuery = WxUserRepo.GetQuery();

            var items = query.Join(wxUserQuery, follow => follow.WxUserId, wxuser => wxuser.Id, (follow, wxuser) => new FollowingItem
            {
                WxUId = follow.WxUserId,
                WxName = wxuser.Nickname,
                Avatar = wxuser.Avatar
            }).ToList();

            return new PagedResultDto<FollowingItem>(total, items);
        }

        /// <summary>
        /// 关注我的
        /// </summary>
        /// <param name="page"></param>
        /// <param name="currentWxUId"></param>
        /// <returns></returns>
        public PagedResultDto<FollowingItem> MyFollower(PagedResultRequestDto page, string currentWxUId)
        {
            var followQuery = FollowRepo.GetQuery().Where(t => t.WxUserId == currentWxUId);

            int total = followQuery.Count();

            followQuery = followQuery.OrderByDescending(t => t.CreateAt).Skip(page.SkipCount).Take(page.MaxResultCount);

            var wxUserQuery = WxUserRepo.GetQuery();

            var items = followQuery.Join(wxUserQuery, follow => follow.FollowBy, wxuser => wxuser.Id, (follow, wxuser) => new FollowingItem
            {
                WxUId = follow.WxUserId,
                WxName = wxuser.Nickname,
                Avatar = wxuser.Avatar
            }).ToList();

            return new PagedResultDto<FollowingItem>(total, items);
        }


    }
}